#This program copy data from a file to another. 
#error control insert data, verify is it ok to put a register with a value to verify the error  

.extern fstat
.section .rodata
sourceMsg: .asciz"1-Insert the complete path of the source file\n"
msgErrorOpen: .asciz"Error when open the file \n"
file: .asciz"destfile.txt"
formatString: .asciz "%s"

.section .bss
sourcefilepath: .space 140
fd: .space 4
buffer: .space 200 
size: .space 200 
.section .text
.globl _start
_start:
      pushl $sourceMsg
      call printf
      addl $4, %esp
      pushl $sourcefilepath  #read the path of the source file
      pushl $formatString #the format is inserted. this is important at the moment of reading something through console
      call scanf #read the value
      addl $8, %esp #clean the stack
      call readFile
      cmpl $1, %ecx
      je  endprogram
call insertData 
      jmp endprogram
endprogram:      movl $1, %eax
      movl $0, %ebx
      int $0x80
  


                                
readFile:  pushl %ebp    # prologue
           movl %esp, %ebp #prologue
          #open the file
           movl $5, %eax #service number 5: open 
           movl $sourcefilepath, %ebx #copy the path into ebx 
           movl $0, %ecx #readonly
           int $0x80 #  SO call
            
          # the file descriptor is in %eax. 
           #this is a number that you use to refer to this file throughout your program.
            movl %eax, fd  
            cmpl $0, %eax
            #jump if the comparison is negative
            js error          

#obtaining the size of the file
            pushl $size 
            pushl fd
            call fstat
            addl  $8, %esp
         
           #now we can read
            movl $3, %eax # service number 3:read
            movl fd, %ebx  # File Descriptor must be in ebx register
            movl size+44, %edx  #size of the file in edx  
            movl $buffer, %ecx #address where we want to save data
            int $0x80   #SO call
            jmp end          
            
            
            
          
error:   pushl $msgErrorOpen
         call printf
         addl $4, %esp
         movl $1, %ecx
         jmp end
           #epilogue
end:  

            movl $6, %eax
            movl fd, %ebx
           int $0x80
        
       leave
           ret

insertData:       pushl %ebp #prologue
                  movl %esp, %ebp #prologue
               
                  #open file in write mode
                  movl $5, %eax #mode=5 open
                  movl $file, %ebx #destiny file
                  movl $02101, %ecx #write mode
                  int $0x80  #s.o call
                 movl %eax, fd   #move eax to fd to avoid losing fd
                
                #write in another file the data copied                 
	         movl $4, %eax  
                 movl fd, %ebx  #fd must be in ebx
		 movl size+44, %edx #bytes to be copied 
		 movl $buffer, %ecx #data to be copied
                 int $0x80 #s.0 call
                 
                #close file
                movl $6, %eax 
                movl fd, %ebx
                int $0x80
                
               #epilogue  
                leave 
                 ret      
         
